<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_services_AliasService extends f_core_Service
{
	/**
	 * @var m_backoffice_services_AliasService
	 */
	private static $instance;
	
	/**
	 * Constructor
	 */
	private function __construct()
	{
		// Code if needed...
	}
	
	/**
	 * @return m_backoffice_services_AliasService
	 */
	public static function getInstance()
	{
		if (self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}

    /**
     * Get authorized document types for alias in a module
     * @param string $moduleName
     * @return array
     */
	public function getAuthorizedAliasDocTypes($moduleName)
    {
        $result = array();

        if(!is_null($moduleName))
        {
            $xmlFile = f_file_Resolver::getModuleFilePath($moduleName, 'config'.DS.'alias.xml');

            if(!is_null($xmlFile))
            {
                $xmlObject = f_xml_Xpath::getInstance($xmlFile);

                $items = $xmlObject->getElementsFromXpath('item');

                if(count($items) > 0)
                {
                    foreach ($items as $item)
                    {
                        /* @var $item f_xml_XmlElement */
                        if($item->getAttributeValue('disabled') == 'true')
                        {
                            $result['disabled'][] = $item->getAttributeValue('documentType');
                        }
                        else
                        {
                            $result['enabled'][] = $item->getAttributeValue('documentType');
                        }
                        $result['all'][] = $item->getAttributeValue('documentType');
                    }
                    return $result;
                }
            }
        }
    }

    public static function getAliasFromDocument($document)
    {
        return f_document_Provider::getInstance()->loadDocuments('backoffice/alias')->whereIsEqual('aliasDocumentId', $document->getUniqueId())->retrieveDocuments($document->getLang());
    }
}